package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;


/**
 * This is the UserPage. The page where users go after they login From here
 * Users can see information that is specific to them.
 * 
 */
public class UserPage extends JPanel {
	private static final long serialVersionUID = -4026277946435130852L;
	JPanel lowerPanel, userPanel, namePasswordPanel, titlePanel,
		majorBooksPanel, classBooksPanel;

	/* titlePanel Components */
	JLabel title, CBtitle, MBtitle;

	/* userPanel Components */
	JLabel majorBooks, classBooks;
	JButton logoutButton, sellButton, userInfoButton;

	/* searchPanel Components */
	SearchPanel searchPanel;
	SearchPanel2 MBsearch, CBsearch;

	// Parent Panel
	GUI guiObject;
	
	public void clearFields()
	{
		searchPanel.data.clear();
		searchPanel.table.updateUI();
	}

	public UserPage(GUI guiObject) {
		setLayout(new GridBagLayout());

		this.guiObject = guiObject;

		//createSellButton();
		createTitlePanel();
		createLowerPanel();
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(0,0,0,0),0,0);

		add(titlePanel, Constraints.gbc);
		
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.SOUTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);

		add(new JSeparator(SwingConstants.HORIZONTAL), Constraints.gbc);

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.NONE, new Insets(5,0,0,5),0,0);

		add(lowerPanel, Constraints.gbc);
		
		return;
	}

	/*private void createSellButton() {
		sellButton = new JButton("Sell");
		sellButton.setActionCommand("Sell");
		sellButton.addActionListener(guiObject);
		add(sellButton, BorderLayout.CENTER);
	}*/

	private void createTitlePanel() {
		titlePanel = new JPanel(new GridBagLayout());

		title = new JLabel("WELCOME USER");
		title.setForeground(Color.white);
		titlePanel.setBackground(Color.blue);
		title.setFont(new Font("Serif", Font.BOLD, 48));

		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		titlePanel.add(title, Constraints.gbc);
	}

	private void createLowerPanel() {
		lowerPanel = new JPanel(new GridBagLayout());
		lowerPanel.setVisible(true);

		createUserPanel();
		searchPanel = new SearchPanel(guiObject, this);

		Constraints.setConstraints(0,0,1,2,0,0,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(searchPanel, Constraints.gbc);
		
		Constraints.setConstraints(1,0,1,2,1,1,GridBagConstraints.EAST,
				GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		lowerPanel.add(new JSeparator(SwingConstants.VERTICAL), Constraints.gbc);

		Constraints.setConstraints(2,0,1,1,0,0,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(userPanel, Constraints.gbc);
		
		ImagePanel image = new ImagePanel("unicorn.jpg");
		
		Constraints.setConstraints(2,1,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.BOTH, new Insets(5,0,0,3),0,0);
		
		lowerPanel.add(image, Constraints.gbc);
	}

	private void createUserPanel() {
		
		userPanel = new JPanel(new GridBagLayout());
		
		//CHANGE THE RIGHT COLUMN
		//Turn the login box into
		//	1. a sell button
		//	2. a Major Books list
		//	3. a Class Books list
		
		/*majorBooksPanel = new JPanel(new GridBagLayout());
			MBtitle = new JLabel("Books for your Major");
			MBtitle.setForeground(Color.white);
			majorBooksPanel.setBackground(Color.blue);
			MBtitle.setFont(new Font("Serif", Font.BOLD, 20));
			Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,
					GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);
			majorBooksPanel.add(MBtitle, Constraints.gbc);
			
			MBsearch = new SearchPanel2(guiObject, this);
			Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.CENTER,
					GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);
			majorBooksPanel.add(MBsearch, Constraints.gbc);
			
			
			
		classBooksPanel = new JPanel(new GridBagLayout());
			CBtitle = new JLabel("Books for your Classes");
			CBtitle.setForeground(Color.white);
			classBooksPanel.setBackground(Color.blue);
			CBtitle.setFont(new Font("Serif", Font.BOLD, 20));
			Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,
					GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);
			classBooksPanel.add(CBtitle, Constraints.gbc);
			
			CBsearch = new SearchPanel2(guiObject, this);
			Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.CENTER,
					GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);
			classBooksPanel.add(CBsearch, Constraints.gbc);
			

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);
		
		userPanel.add(majorBooksPanel, Constraints.gbc);
		
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);
		
		userPanel.add(classBooksPanel, Constraints.gbc);
		
		userPanel.setVisible(true);
		majorBooksPanel.setVisible(true);
		classBooksPanel.setVisible(true);*/
		
		/*
		titlePanel = new JPanel(new GridBagLayout());
		title = new JLabel("WELCOME USER");
		title.setForeground(Color.white);
		titlePanel.setBackground(Color.blue);
		title.setFont(new Font("Serif", Font.BOLD, 48));

		Constraints.setConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER,
				GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

		titlePanel.add(title, Constraints.gbc);*/
		
		/*--ADDING USERINFO BUTTON--*/
		
		JPanel buttonPanel = new JPanel(new GridBagLayout());
		
		userInfoButton = new JButton("User Info");
		userInfoButton.setActionCommand("info");
		userInfoButton.addActionListener(guiObject);
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,43),0,0);
		
		/*--ADDING SELL BUTTON--*/
		buttonPanel.add(userInfoButton, Constraints.gbc);
		
		sellButton = new JButton("Sell");
		sellButton.setActionCommand("sell");
		sellButton.addActionListener(guiObject);
		
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,43),0,0);
		
		buttonPanel.add(sellButton, Constraints.gbc);

		/*--ADDING LOGOUT BUTTON--*/
		
		logoutButton = new JButton("Logout");
		logoutButton.setActionCommand("logout");
		logoutButton.addActionListener(guiObject);
		
		Constraints.setConstraints(2,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,3),0,0);
		
		buttonPanel.add(logoutButton, Constraints.gbc);
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,3),0,0);
		
		userPanel.add(buttonPanel, Constraints.gbc);
	}

}
